<?php
/**
 *
 * @package avp
 * @version $Id: $
 * @copyright (c) 2006 Jan-Willem van Ganswijk, Stefan van Essen
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */
if (!defined('IN_SCRIPT')) {
    die();
}

include($root . 'libs/smarty/Smarty.class.php');

define('FALLBACK_THEME', 'default');

class template {
    var $smarty_class;

    var $tpldir;
    var $fallbackdir;
    var $page_title;
    var $templates;

    function template($tplset)
    {
        global $root;
        if (is_dir($root . 'templates/' . $tplset . '/') || is_dir($root . 'templates/' . FALLBACK_THEME . '/')) {
            $this->tpldir = $root . 'templates/' . $tplset . '/';
            $this->fallbackdir = $root . 'templates/' . FALLBACK_THEME . '/';
        } else {
            die("template.class.php; Error: template directory nor fallback directory can be found");
        }

        $this->smarty_class = new Smarty;
        $this->smarty_class->template_dir = $this->tpldir;
        if (defined('DEBUG'))
            $this->smarty_class->debugging = true;

        if (!is_dir($root . 'templates/compiled/' . $tplset . '/')) {
            mkdir($root . 'templates/compiled/' . $tplset . '/');
        }
        $this->smarty_class->compile_dir = $root . 'templates/compiled/' . $tplset . '/';
        $this->smarty_class->config_dir = $root . 'templates/configs/';
    }
    function set_title($title)
    {
        global $lang;
        $this->page_title = $lang['TITLE_PREFIX'] . $lang['TITLE_SEPARATOR'] . $title;
    }

    function add_templates($templates)
    {
        global $root;
        $templates = explode(',', $templates);
        foreach($templates as $template) {
            if (is_readable($this->tpldir . trim($template) . '.tpl')) {
                $this->templates[] = trim($template) . '.tpl';
            } elseif (is_readable($this->fallbackdir . trim($template) . '.tpl')) {
                $this->templates[] = "../" . FALLBACK_THEME . '/' . trim($template) . '.tpl';
            } else {
                die("\$template->add_template(): $template could not be found in $this->tpldir nor in $this->fallbackdir");
            }
        }
    }

    function append($name, $value)
    {
        $this->smarty_class->append($name, $value);
    }

    function add_var($name, $value)
    {
        $this->smarty_class->assign($name, $value);
    }

    function add_vars($vars)
    {
        if (!is_array($vars)) {
            return false;
        }
        foreach($vars as $name => $value) {
            $this->add_var($name, $value);
        }
    }

    function make_page()
    {
        global $lang, $root;
        $this->smarty_class->config_load('main_urls.conf');
        $this->add_vars(array('lang' => $lang,
                'tpldir' => $this->tpldir,
                'page_title' => $this->page_title,
                'ROOT_DIR' => $root,
                'SCRIPT_VERSION' => SCRIPT_VERSION));

        foreach($this->templates as $template) {
            $this->smarty_class->display($template);
        }
    }

	function get_smarty_version(){
		return $this->smarty_class->_version;
	}
}

?>